package org.hyjiacan.deadqq.server.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hyjiacan.deadqq.common.entity.User;
import org.hyjiacan.deadqq.server.db.DBUtil;


public class ContactDAO implements IContactDAO {

	@Override
	public boolean add(long userNumber, long contactNumber) {
		PreparedStatement ps = DBUtil.getPreparedStatement(DBUtil
				.getString("contact add"));
		try {
			ps.setLong(1, contactNumber);
			ps.setLong(2, userNumber);
			return ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(long userNumber, long contactNumber) {
		PreparedStatement ps = DBUtil.getPreparedStatement(DBUtil
				.getString("contact delete"));
		try {
			ps.setLong(1, contactNumber);
			ps.setLong(2, userNumber);
			return ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean edit(long userNumber, long contactNumber) {
		PreparedStatement ps = DBUtil.getPreparedStatement(DBUtil
				.getString("contact update"));
		try {
			ps.setLong(1, contactNumber);
			ps.setLong(2, userNumber);
			return ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public User queryByNumber(long userNumber, long contactNumber) {
		PreparedStatement ps = DBUtil.getPreparedStatement(DBUtil
				.getString("contact query by number"));
		try {
			ps.setLong(1, contactNumber);
			ps.setLong(2, userNumber);
			ResultSet rs = ps.executeQuery();
			return DBUtil.getUser(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<User> queryAll(long number) {
		PreparedStatement ps = DBUtil.getPreparedStatement(DBUtil
				.getString("contact query all"));
		try {
			ps.setLong(1, number);
			ResultSet rs = ps.executeQuery();
			return DBUtil.getUsers(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
